//
//  SJMainTabBarController.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/9/29.
//  Copyright © 2015年 world. All rights reserved.
//
//  自定义的tabBarController

import UIKit

class SJMainTabBarController: UITabBarController {
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //创建自定义的tabBar
        let tB = SJMainBar()
        //替换系统tabBar
        setValue(tB, forKey: "tabBar")
        
        //给加号添加点击事件
        tB.plusBtn.addTarget(self, action: "pluseBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        //添加子控制器
        addChildViewControllers()
     
    }
    
    //加号按钮的点击事件
    func pluseBtnDidClick(){
        let nav = UINavigationController(rootViewController: SJComposeViewController())
        presentViewController(nav, animated: true, completion: nil)
    }

    
    ///添加tabBarVC的子控制器
    private func addChildViewControllers() {
//        //首页
//        addChildViewController(SJHomeTableViewController(), title: "首页", imageName: "tabbar_home")
//        //消息
//        addChildViewController(SJMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
//        //发现
//        addChildViewController(SJDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
//        //我
//        addChildViewController(SJProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        //加载bundle的路径 返回值类型option
        let path = NSBundle.mainBundle().pathForResource("mainSetting.json", ofType: nil)!
        //加载path下的json文件 返回值类型option
        let data = NSData(contentsOfFile: path)!
        
        //反序列化 json-> data->NSArray
        do{
            //返回anyObject 类型转换为 存放字典的数组   字典中的key 与 value 都是string类型的
            let array =  try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [[String: String]]
            //遍历array
            for dic in array {
//                print(dic)
                addChildViewController(dic["className"]!, title: dic["title"]!, imageName: dic["imageName"]!)
            }
        }catch{
            print(error)
        }
        

    }
    
    ///添加控制器
    private func addChildViewController(className: String,title: String, imageName: String) {
        
        //通过infoDictionary获取命名空间的名称
        //infoDictionary 返回值为optional的  anyObject 
        //转换成字符串 使用关键字 as
        let name = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String;
        //通过命名空间 将字符串 转换 为 对应的类名
        //NSClassFromString 返回值 带? 的anyClass
        let classVC: AnyClass = NSClassFromString(name+"."+className)!
        //通过类名 创建对应类型的 对象
        let vc = (classVC as! UIViewController.Type).init(nibName: nil, bundle: nil)
        //print(VC)
        //设置title
        vc.title = title
        //tabBarButton的属性由对象子控制器的tabBarItem来设置
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
        
        //设置tabBar的图片与文字的颜色
//        tabBar.tintColor = UIColor.orangeColor()
        
        //创建导航控制器
        let nav = UINavigationController(rootViewController: vc)
        //给tabBarvc添加子控制器
        addChildViewController(nav)
    }

}
